package com.poseidon.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ProxyHandle implements InvocationHandler, BaseJDKProxy {

	// 被代理的真实角色
	private Object realObj;

	public ProxyHandle(Object realObj) {
		super();
		this.realObj = realObj;
	}

	/**
	 * JDK被代理的对象必须实现接口
	 * 
	 * 第二个参数：method是代理对象的接口方法 第三个参数：method是被代理对象的参数
	 */
	public Object invoke(Object proxy, Method method, Object[] args) {
		Object invoke = null;
		try {
			before();
			System.out.println("--------------------------------");
			for (Object obj : args) {
				System.out.println("参数：" + obj);
			}
			System.out.println("--------------------------------");
			invoke = method.invoke(realObj, args);
			after();
		} catch (Throwable e) {
			e.printStackTrace();
			exceptionThrow();
		} finally {
			last();
		}
		return invoke;
	}

	public void before() {
		System.out.println("方法调用前：doSomething");

	}

	public void after() {
		System.out.println("方法调用后：doSomething");
	}

	public void last() {
		System.out.println("方法调用完毕：doSomething");
	}

	public void exceptionThrow() {
		System.out.println("方法调用出现异常：doSomething");
	}
}